﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LearnXpath
{
    class Program
    {
        static void Main(string[] args)
        {

            //List<BookModule> bookList = new List<BookModule>();
            //XmlDocument doc = new XmlDocument();

            //doc.Load(@"../bookStore.xml");

            //XmlNode xn = doc.SelectSingleNode("bookstore");

            //foreach (XmlNode node in xn)
            //{
            //    XmlElement ele = node as XmlElement;

            //    BookModule book = new BookModule();
            //    //属性
            //    book.category = ele.GetAttribute("category");


            //    //子节点
            //    XmlNodeList nodeList = ele.ChildNodes;
            //    book.title = nodeList.Item(0).InnerText;
            //    book.lanage = ((XmlElement)nodeList.Item(0)).GetAttribute("lang");
            //    book.author = nodeList.Item(1).InnerText;
            //    book.year = nodeList.Item(2).InnerText;
            //    book.price = nodeList.Item(3).InnerText;
            //    bookList.Add(book);
            //}

            //if (bookList != null && bookList.Count > 0)
            //{
            //    foreach (BookModule b in bookList)
            //    {
            //        Console.WriteLine("书名:" + b.title+" 语言:"+b.lanage+" 作者："+b.author+" 年份："+b.year+" 价格："+b.price);
            //    }
            //}
            //UseXPathWithXmlDocument();


            HttpWebRequest request = WebRequest.Create("http://news.163.com/16/0817/01/BUKPVG2800014AEE.html") as HttpWebRequest;
            request.Method = "get";
            request.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送post请求
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
            //返回结果网页的html代码
            string content = sr.ReadToEnd();
            Console.WriteLine(content);
            Console.ReadLine();

        }

        static void UseXPathWithXmlDocument()
        {
            //加一句注释
            XmlDocument doc = new XmlDocument();
            doc.Load("../bookStore.xml");
            //使用xPath选择需要的节点  
            XmlNodeList nodes = doc.SelectNodes("/bookstore/book");
            foreach (XmlNode item in nodes)
            {
                string title = item.ChildNodes[0].InnerText;
                Console.WriteLine("{0}", title);
            }
        }
    }
}
